﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hospital.Models;

namespace Hospital.Models
{
    public class MenReposite
    {
        private HostleDataContext _datacontext;

        public MenReposite(HostleDataContext datacontext)
        {
            _datacontext = datacontext;
        }

        public IEnumerable<Men> GetMen()
        {
            return _datacontext.Mens.OrderBy(m => m.FIO);
        }

        public Men GetMan(int id)
        {
            return _datacontext.Mens.SingleOrDefault(m => m.Id_Man == id);
        }

        public Men GetLastMan(string polis)
        {
            return _datacontext.Mens.SingleOrDefault(m => m.Polis == polis);
        }

        public Men AddMan(string fio, string adress, string phone, string polis)
        {
            Men m = new Men { FIO = fio, Adress = adress, Phone = phone, Polis = polis};
            _datacontext.Mens.InsertOnSubmit(m);
            _datacontext.SubmitChanges();
            return m;
        }

        public void UpdateMan(Men m)
        {
            Men dbMen = GetMan(m.Id_Man);
            dbMen.AssignedOn = m.AssignedOn;
            dbMen.FIO = m.FIO;
            dbMen.Adress = m.Adress;
            dbMen.Phone = m.Phone;
            dbMen.Polis = m.Polis;
            _datacontext.SubmitChanges();
        }

        public void DeleteMan(int id)
        {
            Men dbMen = GetMan(id);
            _datacontext.Mens.DeleteOnSubmit(dbMen);
            _datacontext.SubmitChanges();
        }
    }
}